package com.xiaojian.blog.controller.admin;

import com.xiaojian.blog.controller.BaseController;
import com.xiaojian.blog.exception.TipException;
import com.xiaojian.blog.model.CommentPO;
import com.xiaojian.blog.model.ContentPO;
import com.xiaojian.blog.model.LogPO;
import com.xiaojian.blog.model.VO.StatisticsVO;
import com.xiaojian.blog.service.LogService;
import com.xiaojian.blog.service.SiteService;
import com.xiaojian.blog.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 后台管理首页
 */
@Controller("adminIntexController")
@RequestMapping("/admin")
@Transactional(rollbackFor = TipException.class)
public class IndexController extends BaseController {
    private Logger LOGGER = LoggerFactory.getLogger(IndexController.class);

    @Resource
    private SiteService siteService;

    @Resource
    private LogService logService;

    @Resource
    private UserService userService;

    /**
     * 页面跳转
     */
    @GetMapping(value = {"", "/index"})
    public String index(HttpServletRequest request) {
        LOGGER.info("Enter admin index method");
        List<CommentPO> comments = siteService.recentComments(5);
        List<ContentPO> contents = siteService.recentContents(5);
        StatisticsVO statistics = siteService.getStatistics();
        //取最新的20条日志
        List<LogPO> logs = logService.getLogs(1, 5);

        request.setAttribute("comments", comments);
        request.setAttribute("articles", contents);
        request.setAttribute("statistics", statistics);
        request.setAttribute("logs", logs);
        LOGGER.info("Exit admin index method");
        return "admin/index";
    }

    /**
     * 个人设置页面
     */
    @GetMapping(value = "profile")
    public String profile() {
        return "admin/profile";
    }

}
